package com.hmall.cart.listener;


import com.hmall.cart.service.ICartService;
import com.hmall.common.utils.UserContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
@Slf4j
@RequiredArgsConstructor
public class TradeCreateListener {
    private final ICartService cartService;

    @RabbitListener( bindings=@QueueBinding(
            value=@Queue(name= "cart.clear.queue" ,durable = "true"),
            exchange = @Exchange(name = "trade.topic" , type = "topic"),
            key = "order.create"
    ))
    public void listenTradeCreate(Collection<Long> itemIds, Message message){

       log.info("用户{}，开始异步清理购物车", UserContext.getUser());
        cartService.removeByItemIds(itemIds);

    }
}
